package com.lanchuan.soft.service.impl;

import com.lanchuan.soft.dao.IEventStepImageDao;
import com.lanchuan.soft.dao.IStatisticsDao;
import com.lanchuan.soft.dao.IUserDao;
import com.lanchuan.soft.entity.*;
import com.lanchuan.soft.service.IStatisticsService;
import com.lanchuan.soft.utils.SystemConstant;
import lombok.extern.log4j.Log4j2;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class StatisticsService implements IStatisticsService {

    private IStatisticsDao statisticsDao;
    private IEventStepImageDao eventStepImageDao;
    private IUserDao userDao;

    @Autowired
    public StatisticsService(IStatisticsDao statisticsDao, IEventStepImageDao eventStepImageDao, IUserDao userDao) {
        this.statisticsDao = statisticsDao;
        this.eventStepImageDao = eventStepImageDao;
        this.userDao = userDao;
    }

    @Override
    public Map<String, Object> findStatisticsGroupByEventType(ParamUser paramUser) {

        Map<String, Object> resultMap = new HashMap<>();

        if (StringUtils.isBlank(paramUser.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            ParamUser pu = new ParamUser();
            pu.setUserId(paramUser.getOperator());
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            List<Statistics> statisticsList = statisticsDao.findGroupByEventType(operator.getOrganization());

            resultMap.put("errcode", SystemConstant.SUCCESS);
            resultMap.put("list", statisticsList);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("StatisticsService / findStatisticsGroupByEventType", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> findStatisticsGroupByDate(ParamUser paramUser) {

        Map<String, Object> resultMap = new HashMap<>();

        if (StringUtils.isBlank(paramUser.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            ParamUser pu = new ParamUser();
            pu.setUserId(paramUser.getOperator());
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            List<Statistics> statisticsList = statisticsDao.findGroupByDate(operator.getOrganization());

            resultMap.put("errcode", SystemConstant.SUCCESS);
            resultMap.put("list", statisticsList);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("StatisticsService / findStatisticsGroupByDate", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> findStatisticsGroupByTotal(ParamUser paramUser) {

        Map<String, Object> resultMap = new HashMap<>();

        if (StringUtils.isBlank(paramUser.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            ParamUser pu = new ParamUser();
            pu.setUserId(paramUser.getOperator());
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            Statistics statistics = statisticsDao.findByTotal(operator.getOrganization());

            resultMap.put("errcode", SystemConstant.SUCCESS);
            resultMap.put("data", statistics);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("StatisticsService / findStatisticsGroupByTotal", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> findImageListLimit(ParamUser paramUser) {

        Map<String, Object> resultMap = new HashMap<>();

        if (StringUtils.isBlank(paramUser.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            List<EventStepImage> eventStepImageList = eventStepImageDao.findListLimit();

            resultMap.put("errcode", SystemConstant.SUCCESS);
            resultMap.put("list", eventStepImageList);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("StatisticsService / findImageListLimit", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }
}
